package org.tabletopmachine.states.mainmenu;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.StateBasedGame;
import org.tabletopmachine.IMenu;
import org.tabletopmachine.match.Player;
import org.tabletopmachine.twl.BasicTWLGameState;

import de.matthiasmann.twl.Widget;

public class MainMenuState extends BasicTWLGameState {
	private static int _id = 0;

	private GameContainer _gc;
	private StateBasedGame _sbg;
	private MainMenu _mainMenu;
	private NewGameMenu _newGameMenu;

	public MainMenuState() {

	}

	public void init(GameContainer gc, StateBasedGame sbg)
			throws SlickException {
		this.setGc(gc);
		this.setSbg(sbg);

		enterMainMenu();
	}

	public void render(GameContainer arg0, StateBasedGame arg1, Graphics arg2)
			throws SlickException {

	}

	public void update(GameContainer arg0, StateBasedGame arg1, int arg2)
			throws SlickException {
		if (_newGameMenu != null)
			_newGameMenu.update();
		if (_mainMenu != null)
			_mainMenu.update();
	}

	public int getID() {
		return _id;
	}

	public void mouseClicked(int button, int x, int y, int clickCount) {

	}

	public void keyPressed(int key, char c) {

	}

	public GameContainer getGc() {
		return _gc;
	}

	private void setGc(GameContainer gc) {
		_gc = gc;
	}

	public StateBasedGame getSbg() {
		return _sbg;
	}

	private void setSbg(StateBasedGame sbg) {
		_sbg = sbg;
	}

	public void enterNewGameMenu() throws IllegalStateException {
		_newGameMenu = new NewGameMenu(this);
		_newGameMenu.loadUnits();
		enterMenu(_newGameMenu);
	}

	public void enterManageArmyMenu(Player player) {
		ManageArmyMenu MAM = new ManageArmyMenu(this, player);
		MAM.loadUnits();
		enterMenu(MAM);
	}

	public void enterMainMenu() {
		_mainMenu = new MainMenu(this);
		enterMenu(_mainMenu);
	}

	private void enterMenu(IMenu menu) throws IllegalStateException {
		if (!(menu instanceof Widget))
			throw new IllegalStateException("!(menu instanceof Widget)");
		menu.init();
		getRootPane().removeAllChildren();
		getRootPane().add((Widget) menu);
	}
}
